<?php
	class Trivia extends MY_Controller{
		
		public function __construct(){
			parent::__construct();
			$this->load->model('trivia_model');
			$this->load->model('question_model');
		}
		
		public function index($limit=FALSE){
			$this->load->helper("form");
			//set the limit or offset for each page
			$this->trivia_model->pager($limit);
			
			// define the link for pagination
			$my_link = site_url('trivia');		
		
			$data['trivia'] = $this->trivia_model->get_trivia();
			
			// call the pagination from parent controller			
			$this->pager($my_link, $this->trivia_model->get_total(), $this->trivia_model->get_record_per_page());
			
			$data['title'] = "Trivia game list";
			$this->load->view('templates/header1', $data);
			$this->load->view('trivia/index', $data);
			$this->load->view('templates/right', $data);
			$this->load->view('templates/footer');			
		}
		
		public function search($src=FALSE,$limit=FALSE){
			$this->load->helper("form");
			$data['title'] = "Trivia game list";
			$search_text = '';
			
			//set the limit or offset for each page
			$this->trivia_model->pager($limit);			
			
			if($src!=FALSE){				
				$data['trivia'] = $this->trivia_model->search($src);		
				$search_text = $src;
			}
			else{
				$search_text = $this->input->post('search_text');				
				$data['trivia'] = $this->trivia_model->search($search_text);		
			}
			
			// define the link for pagination
			$my_link = site_url('trivia/search/'.$search_text);
			
			// call the pagination from parent controller get_total_search
			$this->pager($my_link, $this->trivia_model->get_total_search($search_text), $this->trivia_model->get_record_per_page(), 4);
			
			$this->load->view('templates/header1', $data);
			$this->load->view('trivia/index', $data);
			$this->load->view('templates/right', $data);
			$this->load->view('templates/footer');
		}
		
		public function displayForm($id=FALSE){			
				$this->load->helper("form");
				$data = array();
				if($id!=FALSE){
					$data['trivia'] = $this->trivia_model->get_trivia($id);				
				}				
				$data['title']="Create Trivia  Game";
				$this->load->view('templates/header1', $data);
				$this->load->view('trivia/form', $data);
				$this->load->view('templates/right', $data);
				$this->load->view('templates/footer');
			
		}
		
		public function create(){
			$this->displayForm();
		}
		
		public function edit($id){
			$this->displayForm($id);
		}
		
		public function delete($id){
			$this->trivia_model->delete($id);
			redirect("trivia", "refresh");
		}
		
		public function save(){			
			
			$this->load->helper("form");
			$this->load->library('form_validation');
			$this->form_validation->set_rules('title', 'Title', 'required');
			if ($this->form_validation->run() === FALSE){
				$id = $this->input->post('id');
				if(!empty($id))
					$data['trivia'] = $this->trivia_model->get_trivia($id);	
				$data['title']="Create Trivia  Game";
				$this->load->view('templates/header1', $data);
				$this->load->view('trivia/form', $data);
				$this->load->view('templates/right', $data);
				$this->load->view('templates/footer');
			}
			else{
				$this->trivia_model->save();
				redirect("trivia", "refresh");
			}			
		}
		
		public function get_question(){
			$trivia_id = $this->input->post('tid');
			
			$arr = $this->question_model->get_question_list($trivia_id);
			if(count($arr)) echo json_encode($arr);
			else echo json_encode(array('1'=>'no data'));
		}
		
		public function assign(){
			$this->load->helper("form");
			$this->load->library('form_validation');
			$data['trivia'] = $this->trivia_model->get_trivia_list();
			$data['title']="Assign game to device";
			$this->load->view('templates/header1', $data);
			$this->load->view('trivia/assign', $data);
			$this->load->view('templates/right', $data);
			$this->load->view('templates/footer');			
		}
		
		public function save_assign(){
			$this->load->helper("form");
			$this->load->library('form_validation');
			$this->form_validation->set_rules('id', 'Trivia selected', 'required');
			$this->form_validation->set_rules('q_id', 'Question selected', 'required');
			if ($this->form_validation->run() === FALSE){
				$this->assign();
			}
			else{
				$this->trivia_model->save_assign();
				redirect('trivia', 'refresh');
			}
		}	
		
	}
?>